﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class DuyetPhieuThanhToan : UserControl
    {
        public DuyetPhieuThanhToan()
        {
            InitializeComponent();
        }

        private void DuyetPhieuThanhToan_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var listPhieuThanhToan = from qt in dc.QuyTrinhMuaHangs
                                     where qt.deleted == false &&
                                           qt.TrangThai == _DB.TrangThaiQuyTrinhMuaHang.CHO_DUYET_PHIEU_THANH_TOAN
                                     orderby qt.PhieuThanhToan.NgayTao descending
                                     select qt.PhieuThanhToan;

            dgvPhieuThanhToan.Rows.Clear();
            int count = 1;
            foreach (_DB.PhieuThanhToan p in listPhieuThanhToan)
            {
                _DB.QuyTrinhMuaHang qt = p.QuyTrinhMuaHangs.Single();
                float tienThanhToan = qt.PhieuMuaHang.getTongTien();
                if (qt.PhieuTamUng != null)
                    tienThanhToan -= qt.PhieuTamUng.SoTien;

                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvPhieuThanhToan);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = p.MaPhieu;
                r.Cells[2].Value = p.NguoiTao.Ten;
                r.Cells[3].Value = p.NgayTao;
                r.Cells[4].Value = (tienThanhToan > 0) ? "Phải trả  " + tienThanhToan.ToString() : "Phải thu  " + (-tienThanhToan).ToString();

                dgvPhieuThanhToan.Rows.Add(r);
            }
        }

        private void btnDuyet_Click(object sender, EventArgs e)
        {
            // check select
            if (dgvPhieuThanhToan.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show("Chắc chắn", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // go
            string maPhieu = (string)dgvPhieuThanhToan.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuThanhToan == maPhieu);
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.DA_DUYET_PHIEU_THANH_TOAN;
            qt.PhieuThanhToan.MaNguoiDuyet = Program.MaNguoiDungHienTai;
            qt.PhieuThanhToan.NgayDuyet = DateTime.Now;

            // auto ket thuc quy trinh
            bool tuDongKetThuc = true;
            foreach (_DB.PhieuDeNghiMuaHang_HangHoa h in qt.PhieuDeNghiMuaHang.PhieuDeNghiMuaHang_HangHoas)
            if (h.SoLuongNhapKho < h.SoLuongDeNghi)
                tuDongKetThuc = false;
            if (tuDongKetThuc)
                qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.KET_THUC;

            dc.SubmitChanges();
            loadData();
        }

        private void btnKhongDuyet_Click(object sender, EventArgs e)
        {
            // check select
            if (dgvPhieuThanhToan.SelectedRows.Count <= 0)
                return;

            // Input ly do
            OtherForms.InputTextForm tf = new OtherForms.InputTextForm("", "Lý do không duyệt");
            tf.ShowDialog();
            if (tf.OK == false)
                return;
            string lydo = tf.getText();

            // go
            string maPhieu = (string)dgvPhieuThanhToan.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuThanhToan == maPhieu);
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_THANH_TOAN;
            qt.PhieuThanhToan.MaNguoiDuyet = Program.MaNguoiDungHienTai;
            qt.PhieuThanhToan.NgayDuyet = DateTime.Now;
            qt.PhieuThanhToan.LyDoKhongDuyet = lydo;
            dc.SubmitChanges();
            loadData();
        }

        private void dgvPhieuThanhToan_DoubleClick(object sender, EventArgs e)
        {
            if (dgvPhieuThanhToan.SelectedRows.Count > 0)
            {
                string maPhieu = (string)dgvPhieuThanhToan.SelectedRows[0].Cells[1].Value;
                OtherForms.XemPhieuThanhToan f = new OtherForms.XemPhieuThanhToan(maPhieu);
                f.Show();
            }
        }
    }
}
